/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.HoaDon;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class HoaDonDAO {
     public static List<HoaDon> layDanhHoaDon() {
        List<HoaDon> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select hd from HoaDon hd";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }   
        return ds;        
    }
    
    public static HoaDon layThongTinHoaDon(int maHoaDon) {
        HoaDon hd = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            hd = (HoaDon) session.get(HoaDon.class, maHoaDon);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return hd;
    }
    
    public static void themHoaDon(HoaDon hd) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(hd);
        session.getTransaction().commit();
        session.close(); 
    }
}
